package srms.the504div1;

import java.util.ArrayList;

public class MathContest {
	private int shownNumber = 0;
	private ArrayList<Boolean> list = new ArrayList<Boolean>();

	public int countBlack(String ballSequence, int repetitions) {
		shownNumber = 0;
		
		char[] balls = ballSequence.toCharArray();

		for (int i = 0; i < repetitions; i++) {
			for (int j = 0; j < balls.length; j++) {
				if (balls[j] == 'B')
					list.add(true);// block
				else
					list.add(false);// white
			}
		}

		while (list.size() > 0) {
			if (list.get(0))
				inverted();
			else
				reversed();
		}

		return shownNumber;
	}

	private void reversed() {
		list.remove(0);
		final int mid = list.size() / 2;
		for (int i = 0, j = list.size() - 1; i < mid; i++, j--) {
			if (i < j) {
				boolean temp = list.remove(i);
				list.add(i, list.remove(j - 1));
				list.add(j, temp);
			}
		}
	}

	private void inverted() {
		shownNumber++;
		ArrayList<Boolean> list1 = new ArrayList<Boolean>();
		for (int i = 1; i < list.size(); i++) {
			list1.add(!list.get(i));
		}
		list = list1;
	}
}
